// *************************************
// EFMP Fleet
// Table 1.do
// *************************************

/*
// Description
// ----------------------------
This program analyses the Enhanced Fleet Modernization Program (EFMP) in 
California and its effect on the fleet.  It creates Table 1.
*/

/*** START CODE ***/

local gpm_round = .01
di `gpm_round'
local mpg_round = 5
di `mpg_round'

	import delimited using "${EFMPFleetData}/vehicles.csv", clear varnames(1)
	keep make model year comb08 atvtype fueltype
	gen VehicleType = atvtype
	replace VehicleType = "ICE" if !regexm(atvtype, "Hybrid") & atvtype != "EV"
	replace VehicleType = "BEV" if atvtype == "EV"
	replace VehicleType = "PHEV" if atvtype == "Plug-in Hybrid"
	replace VehicleType = "HEV" if VehicleType == "Hybrid"
	replace make = upper(trim(make))
	replace model = upper(trim(model))
	replace make = "MCLAREN" if make == "MCLAREN AUTOMOTIVE"
	ren year modelyr
	ren comb08 mpg
	gen gpm = 1/mpg
	
	gcollapse (mean) gpm, by(make modelyr VehicleType)
	tempfile fueleconomy_EFMP
	save `fueleconomy_EFMP', replace
	
	use "${EFMPFleetData}/EFMP_Incentives_precollapse.dta", clear
	
	sort ZipCode CalendarYear Quarter VehicleType New

	ren ReplacementVehicleMake make 
	ren ReplacementVehicleMY modelyr
	
	replace make = upper(make)
	replace make = "CHEVROLET" if make == "CHEVY"
	replace make = "NISSAN" if make == "NISSSAN"
	replace make = "TOYOTA" if make == "PRIUS"
	replace make = "MERCEDES-BENZ" if make == "MERCEDES"
	replace make = "" if make == "N/A"
	
	replace VehicleType = proper(VehicleType)
	replace VehicleType = upper(VehicleType) if inlist(VehicleType, "Phev", "Bev") == 1
	replace VehicleType = "ICE" if VehicleType == "Minivan"
	replace VehicleType = "ICE" if VehicleType == "Conventional"
	replace VehicleType = "HEV" if VehicleType == "Hybrid" | VehicleType == "Hev" 
	drop if mi(make) == 1
	drop if mi(modelyr) == 1
	drop if mi(VehicleType) == 1
	
	replace VehicleType = "BEV" if make == "FIAT" & VehicleType == "PHEV"
	
	
	tempfile precollapse_EFMPdata
	save `precollapse_EFMPdata', replace
	
	
	foreach i in TotalIncentive BaseIncentiveSUM PlusUpIncentiveSUM {
		replace `i'=. if `i'==0
		bysort make modelyr VehicleType ZipCode CalendarYear Quarter: gegen cnt`i'=count(`i')
			
			glevelsof VehicleType, local(vehicletypes_local) // use this local later in the code
			foreach type of local vehicletypes_local {
				bysort ZipCode CalendarYear Quarter: gegen cnt`i'`type' = count(`i') if VehicleType == "`type'"
				gen `i'`type' = `i' if VehicleType == "`type'"
			}
	}	
	gcollapse (sum) TotalIncentive* BaseIncentiveSUM* PlusUpIncentiveSUM* ///
	(mean) cntTotalIncentive* cntBaseIncentiveSUM* cntPlusUpIncentiveSUM*, by(make modelyr VehicleType ZipCode CalendarYear Quarter)

	foreach var of varlist cntTotalIncentive* cntBaseIncentiveSUM* cntPlusUpIncentiveSUM* {
		replace `var' = 0 if `var' == .
	}
	ren ZipCode OwnerZipCode

	drop if OwnerZipCode ==.
	tostring OwnerZipCode, replace
	compress
	tempfile EFMP_count
	save `EFMP_count', replace

	

	merge m:1 make modelyr VehicleType using `fueleconomy_EFMP'
	keep if _merge == 3
	drop _merge
	ren CalendarYear year
	ren Quarter quarter

	replace gpm = round(gpm, `gpm_round')
	drop make modelyr VehicleType
	gcollapse (sum) cntPlusUpIncentiveSUM cntBaseIncentiveSUM, by(OwnerZipCode year quarter gpm)

	compress
	tempfile EFMP_Incentives
	save `EFMP_Incentives', replace
	
	use `precollapse_EFMPdata', clear
	drop if CalendarYear > 2017 
	foreach i in TotalIncentive BaseIncentiveSUM PlusUpIncentiveSUM {
		replace `i'=. if `i'==0
		bysort make modelyr VehicleType: gegen cnt`i'=count(`i')
			
			glevelsof VehicleType, local(vehicletypes_local) // use this local later in the code
			foreach type of local vehicletypes_local {
				bysort ZipCode CalendarYear Quarter: gegen cnt`i'`type' = count(`i') if VehicleType == "`type'"
				gen `i'`type' = `i' if VehicleType == "`type'"
			}
	}	
	gcollapse (sum) TotalIncentive* BaseIncentiveSUM* PlusUpIncentiveSUM* ///
	(mean) cntTotalIncentive* cntBaseIncentiveSUM* cntPlusUpIncentiveSUM*, by(make modelyr VehicleType)

	foreach var of varlist cntTotalIncentive* cntBaseIncentiveSUM* cntPlusUpIncentiveSUM* {
		replace `var' = 0 if `var' == .
	}
	** merge with fueleconomy.gov data
	merge m:1 make modelyr VehicleType using `fueleconomy_EFMP'
	keep if _merge == 3
	drop _merge
	compress
	
	ren cntPlusUpIncentiveSUM count_plusup
	la var count_plusup "Count plusup"
	summ gpm
	
	tempfile before_export_efmp
	save `before_export_efmp', replace
	
	gen gpm_cnt = gpm*count_plusup
	
	preserve
		gcollapse (mean) mean_gpm = gpm (max) max_gpm=gpm (min) min_gpm=gpm (sd) sd_gpm=gpm (count)count_plusup [fweight=count_plusup],
		gen VehicleType = "All Vehicles"
		tempfile EFMPalltypesSummstat
		save `EFMPalltypesSummstat', replace
	restore
	
	gcollapse (mean) mean_gpm = gpm (max) max_gpm=gpm (min) min_gpm=gpm (sd) sd_gpm=gpm (count) count_plusup [fweight=count_plusup], by(VehicleType)
	append using `EFMPalltypesSummstat'
	
	label var count_plusup "Number of Vehicles"
	label var mean_gpm "Mean GPM"
	
	foreach x in mean_gpm sd_gpm min_gpm max_gpm {
		replace `x' = round(`x',.0001)
	}
	
	list  VehicleType count_plusup mean_gpm sd_gpm min_gpm max_gpm
	
	
	/*Table 1: EFMP Vehicle Fuel Efficiency (2015-2017)*/
	texsave VehicleType count_plusup mean_gpm using "${EFMPFleetRes}/EFMP_implied_gpm.tex" if  VehicleType~="ICE", ///
	varlab frag replace
		
	texsave VehicleType count_plusup mean_gpm sd_gpm min_gpm max_gpm using "${EFMPFleetRes}/EFMP_implied_gpm_expanded.tex" if  VehicleType~="ICE", ///
	varlab frag replace
	
	/*Table 1: CA average (2015-2017)*/
	use "${EFMPFleetData}/Before_collapse_tract_quarter.dta", clear
	keep if inlist(year,2015,2016,2017)
	gen cnt_wgt_gpm = gpm*cnt_mmy
	gen cnt_mmy_nonmissingfe = cnt_mmy if gpm~=.
	gcollapse (mean) mean_gpm = gpm (max) max_gpm=gpm (min) min_gpm=gpm (sd) sd_gpm=gpm (count) cnt_mmy cnt_mmy_nonmissingfe [fweight=cnt_mmy]
	
	foreach x in mean_gpm sd_gpm min_gpm max_gpm {
		replace `x' = round(`x',.0001)
	}
	

	/*Hand enter averages into Table 1*/

	
	



